package com.melichercik.code;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Align;

public class HelperBrick extends Brick {
	
	public Brick parent;
	
	public HelperBrick(String type, Brick parent) {
		super(type);
		this.parent = parent;
	}
	
	public synchronized void render(Canvas canvas, int offsetX, int offsetY, int scale) {
		
		Paint myPaint = new Paint();
		
		int realX = this.xx = (x * scale) + (x * scale / 2) + offsetX;
		int realY = this.yy = (y * scale) + (y * scale / 2) + offsetY;
		this.scale = scale;
		
		int padding = getPadding(type);
		
		myPaint.setColor(getColor(type));
		RectF rect = new RectF(realX + padding, padding + realY, realX + scale - padding, realY + scale - padding);
		canvas.drawRoundRect(rect, (float) scale / 10,(float) scale / 10, myPaint);
		
		if (type == ENDIF) {
			int lineSize = scale / 20;
			
			myPaint.setColor(Color.rgb(100, 100, 100));
			canvas.drawRect(realX + scale / 2 - lineSize,
				realY + scale,
				realX + scale / 2 + lineSize,
				realY + scale + scale / 2,
				myPaint
			);
		}
		
		int lineHeight = (int) (getFontSize(type) * ((float) scale / 200));
		
		int realCaptionX = (x * scale) + (scale / 2) + (x * scale / 2) + offsetX;
		int realCaptionY = (int) ((y * scale) + ((scale / 2) * 1.1) + (y * scale / 2) + offsetY);
		
		int captioFontSize = (int) (getFontSize(type) * ((float) scale / 200));
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setTextSize(captioFontSize);
		myPaint.setTextAlign(Align.CENTER);
		canvas.drawText(getCaption(), realCaptionX, realCaptionY, myPaint);
	}
	
	private int getColor(String type) {
		int color = 0;
		
		if (type == ELSE || type == ENDELSE || type == THEN || type == ENDTHEN || type == ENDIF) {
			color = Color.rgb(251, 154, 62);
		}
		
		return color;
	}
	
	private int getPadding(String type) {
		int padding = 0;
		
		if (type == ELSE || type == ENDELSE || type == THEN || type == ENDTHEN) {
			padding = scale / 6;
		}
		
		return padding;
	}
	
	private int getFontSize(String type) {
		int size = 40;
		
		if (type == ELSE || type == ENDELSE || type == THEN || type == ENDTHEN) {
			size = 30;
		}
		
		return size;
	}
}
